<?php
namespace common\helpers;

use Yii;
use linslin\yii2\curl;
use Exception;

class RestHelper
{
    /**
     * 调用外部REST API.数据格式Json
     *
     * @param      $method
     * @param      $url
     * @param null $body
     * @param null $header
     *
     * @return mixed|null
     * @throws Exception
     */
    public static function callRestAPIJson($method, $url, $body = null, $header = null)
    {
        $curl     = new curl\Curl();
        $response = null;
        if (isset($header)) {
            $curl->setOptions($header);
        }

        $method = strtoupper($method);

        if ($method == 'GET') {
            $response = $curl->get($url);
        } elseif ($method == "POST") {
            $response = $curl->setOption(
                CURLOPT_POSTFIELDS,
                $body
            )->post($url);
        } elseif ($method == "PUT") {
            $response = $curl->setOption(
                CURLOPT_POSTFIELDS,
                $body
            )->put($url);
        } elseif ($method == "DELETE") {
            $response = $curl->delete($url);
        } else {
            throw new Exception("Not support method.");
        }

        return $response;
    }

    /**
     * 调用外部REST API.数据格式Form
     *
     * @param      $method
     * @param      $url
     * @param null $body
     * @param null $header
     *
     * @return mixed|null
     * @throws Exception
     */
    public static function callRestAPIForm($method, $url, $body = null, $header = null)
    {
        $curl     = new curl\Curl();
        $response = null;
        if (isset($header)) {
            $curl->setOptions($header);
        }

        $method = strtoupper($method);

        if ($method == 'GET') {
            $response = $curl->get($url);
        } elseif ($method == "POST") {
            $response = $curl->setOption(
                CURLOPT_POSTFIELDS,
                http_build_query($body)
            )->post($url);
        } elseif ($method == "PUT") {
            $response = $curl->setOption(
                CURLOPT_POSTFIELDS,
                http_build_query($body)
            )->put($url);
        } elseif ($method == "DELETE") {
            $response = $curl->delete($url);
        } else {
            throw new Exception("Not support method.");
        }

        return $response;
    }

    public static function postWithForm($url, $body, $header = null)
    {
        return self::callRestAPIForm('POST', $url, $body, $header);
    }

    public static function putWithForm($url, $body, $header = null)
    {
        return self::callRestAPIForm('PUT', $url, $body, $header);
    }

    public static function postWithJson($url, $body, $header = null)
    {
        return self::callRestAPIJson('POST', $url, $body, $header);
    }

    public static function putWithJson($url, $body, $header = null)
    {
        return self::callRestAPIJson('PUT', $url, $body, $header);
    }

    public static function postAsync($url, $body)
    {
        /* $post_string = json_encode($body, JSON_UNESCAPED_UNICODE);

         $parts = parse_url($url);

         $fp = fsockopen($parts['host'], isset($parts['port']) ? $parts['port'] : 80, $errno, $errstr, 30);
         if (!$fp) {
             throw new Exception(sprintf("Host[%s]-[%s]:%s", $parts['host'], $errno, $errstr));
         }

         $out = "POST " . $parts['path'] . " HTTP/1.1\r\n";
         $out .= "Host: " . $parts['host'] . "\r\n";
         $out .= "Content-Type: application/json\r\n";
         $out .= "Content-Length: " . strlen($post_string) . "\r\n";
         $out .= "Connection: Close\r\n\r\n";
         if (isset($post_string)) {
             $out .= $post_string;
         }

         fwrite($fp, $out);
         fclose($fp);*/

        $post_string = json_encode($body, JSON_UNESCAPED_UNICODE);
        $cmd         = sprintf("curl %s -d '%s' > /dev/null 2>&1 &", $url, $post_string);

        shell_exec($cmd);
    }
}
